# -*- coding: UTF-8 -*-

"""
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import datetime

from django.core.urlresolvers import reverse
from django.contrib.syndication.feeds import Feed
from django.utils.translation import ugettext as _

from psp.models import *

class Polls(Feed):
    link = "/hlasovani/"
    title_template = "krmeni/poll_title.html"
    description_template = "krmeni/poll_description.html"

    def get_object(self, bits):
        return Poll.objects.order_by('-date')[0]


    def title(self, obj):
        return _(u"Výběr výsledků hlasování %s") % obj.date.strftime("%e %B %Y")


    def description(self, obj):
        retVal = _(u"""
                    Zajímáte-li se o výsledky aktuálně projednávaných návrhů, pak
                    děláte více než dobře, když svou RSS čtečku krmíte tímhle
                    žrádlem.
                    """
                )
        return retVal


    def items(self):
        polls = Poll.objects.order_by('-date').filter(
                                        ~Q(name__iexact = 'Procedurální hlasování'),
                                        ~Q(name__iexact = '')
                                    )[:8]
        return polls

    
    def item_link(self, obj):
        return reverse('psp.views.show_poll', args = [ str(obj.id) ])

class Person(Feed):
    title = _(u"Změny v poslaneckých lavicích")
    link = "/poslanci/"
    description = _(u"""
                    Všechny důležité novinky o změnách v poslaneckých řadách,
                    které by neměly nikomu uniknout.  Vzniky a zániky členství
                    ve výborech, skupinách, partajních klubech, všechna
                    prostřídání v poslaneckých lavicích, nic z toho vám
                    neunikne, pokud svou čtečku krmíte z tohoto zdroje.
                    """
                )
    title_template = "krmeni/person_title.html"
    description_template = "krmeni/person_description.html"
    
    def items(self):
        memberships = Membership.objects.order_by('-end', '-start')[:8]
        return memberships


    def item_link(self, obj):
        return reverse('psp.views.show_mp_term', args = [
                                            obj.person.slug,
                                            str(obj.group.term.getStart()),
                                            str(obj.group.term.getEnd()),
                                        ]
                        )

    def item_pubdate(self, obj):
        if obj.end:
            return datetime.datetime(
                                year = obj.end.year,
                                month = obj.end.month,
                                day = obj.end.day
                            )
        else:
            return datetime.datetime(
                                year = obj.end.year,
                                month = obj.end.month,
                                day = obj.end.day
                            )
